
import Vue from 'vue'
import { ISLOGINUSER, NOLOGINUSER, USERIDFN, CHECKFIRSTLOGIN, GETPERMISSIONLIST, GETUERINFO } from '../mutations-type'
import { getBasicUserInfo,listMyFunctionCodes,getAreaDataList } from '@/api/sysSetting.js'
import initToken from "@/libs/util.token.js";
import {isObject} from "@/libs/tool";

// 登录状态
const state = {
  isLogin: false,
  userInfo: {},
  userId: null,
  firstLogin: false,
  permissionList: []
}
const getters = {
  firstLogin: state => state.firstLogin,
  permissionList: state => state.permissionList
}
const actions = {
  // getSignIn ({ state, commit }, flag) {
  //   commit('ISLOGINUSER', flag)
  // },
  // getSignUp ({ state, commit }, flag) {
  //   // console.log(flag)
  //   commit('NOLOGINUSER', flag)
  // },
  /**
   * 存储登录人信息
   * @param {*} param0
   */
  setUserInfo ({ state, dispatch }) {
    getBasicUserInfo().then(res => {
      if(res.code===200){
        return new Promise(async resolve => {
          // store 赋值
          state.userInfo = res.result;
          // 持久化
          await dispatch('admin/db/set', {
              dbName: 'sys',
              path: 'userInfo',
              value: res.result,
              user: true
          }, { root: true });
          // end
          resolve();
      })
      }
    })
  },
  /**
   * 获取登录人信息
   * @param {*} param0
   */
  getUserInfo({ state, dispatch }) {
        return new Promise(async resolve => {
           // store 赋值
            state.userInfo = await dispatch('admin/db/get', {
              dbName: 'sys',
              path: 'userInfo',
              defaultValue: {},
              user: true
          }, { root: true });
          // end
          resolve();
      })

  },
    /**
   * 清空登录人数据
   * @param {*} param0
   * @param {*}
   */
  clearUserInfo({state, dispatch},userInfo) {
    return new Promise(async resolve => {
      // store 赋值
      state.userInfo = userInfo
      // 持久化
      await dispatch('admin/db/set', {
          dbName: 'sys',
          path: 'userInfo',
          value: userInfo,
          user: true
      }, { root: true });
      // end
      resolve();
  })
  },
  getUserId ({ state, commit }, flag) {
    sessionStorage.setItem('custId', flag)
    commit('USERIDFN', flag)
  },
  // 存储权限 数据
  listMyFunctionCodes ({ state, dispatch }) {
    listMyFunctionCodes().then(res=>{
      if(res.code===200){
        return new Promise(async resolve => {
          // store 赋值
          state.permissionList = Array.isArray(res.result) ? res.result : [res.result];
          // 持久化
          await dispatch('admin/db/set', {
              dbName: 'sys',
              path: 'permissionList',
              value: Array.isArray(res.result) ? res.result : [res.result],
              user: true
          }, { root: true });
          // end
          resolve();
      })
      }
    })
  },
   /**
   * 获取权限数据
   * @param {*} param0
   */
  getPermissionList ({state, dispatch}) {
    return new Promise(async resolve => {
      // store 赋值
      state.permissionList = await dispatch('admin/db/get', {
          dbName: 'sys',
          path: 'permissionList',
          defaultValue: [],
          user: true
      }, { root: true });
      // end
      resolve();
    })
  },
   /**
   * 清空权限数据
   * @param {*} param0
   * @param {*}
   */
  clearPermissionList({state, dispatch},permissionList) {
    return new Promise(async resolve => {
      // store 赋值
      state.permissionList = permissionList
      // 持久化
      await dispatch('admin/db/set', {
          dbName: 'sys',
          path: 'permissionList',
          value: permissionList,
          user: true
      }, { root: true });
      // end
      resolve();
  })
  },

  /**
   * 存储区域数据
   * @param {*} param0
   */
  setAreaData ({ state, dispatch }) {
    const custInfo = initToken.initToken()
    console.log(custInfo,"store--custInfo")
    if(custInfo.orgNo) {
      getAreaDataList({orgNo: custInfo.orgNo, level: "04"}).then(res=>{
            var res = res
            const areaList = []

            if(isObject(res)) {
              loop([res],areaList)
            }
            else{
              loop(res,areaList)
            }
            function loop(arr,areaList) {
              arr.map((item,index)=>{
                areaList[index] = {
                    label: item.areaName,
                    id: item.areaNo,
                }
                if(item.children && item.children.length>0) {
                  areaList[index].children = []
                  loop(item.children,areaList[index].children)
                }
              })
            }
            console.log(areaList,'初始化区域树结构')
            return new Promise(async resolve => {
              // store 赋值
              state.areaList = areaList
              // 持久化
              await dispatch('admin/db/set', {
                  dbName: 'sys',
                  path: 'areaList',
                  value: areaList,
                  user: true
              }, { root: true });
              // end
              resolve();
            })

          })
        }
  },
   /**
   * 获取区域数据
   * @param {*} param0
   */
  getAreaList ({state, dispatch}) {
    return new Promise(async resolve => {
      // store 赋值
      state.areaList = await dispatch('admin/db/get', {
          dbName: 'sys',
          path: 'areaList',
          defaultValue: [],
          user: true
      }, { root: true });
      // end
      resolve();
    })
  },
   /**
   * 清空区域数据
   * @param {*} param0
   * @param {*}
   */
  cleaAreaList({state, dispatch},areaList) {
    return new Promise(async resolve => {
      // store 赋值
      state.areaList = areaList
      // 持久化
      await dispatch('admin/db/set', {
          dbName: 'sys',
          path: 'areaList',
          value: areaList,
          user: true
      }, { root: true });
      // end
      resolve();
  })
  }
}
const mutations = {
  [GETUERINFO] (state, list) {
    state.userInfo = list
  },
  [NOLOGINUSER] (state, flag) {
    // console.log(flag,'flag')
    state.userInfo = {}
    state.userInfo = flag
    // sessionStorage.removeItem('isLogin', flag.isLogin)
  },
  [CHECKFIRSTLOGIN] (state, flag) {
    state.firstLogin = flag
  },
  [USERIDFN] (state, id) {
    console.log('is', id)
    state.userId = id
  },
  [GETPERMISSIONLIST] (state, list) {
    state.permissionList = list
    console.log( state.permissionList," state.permissionList")
  }
}
export default {
  namespaced: true,
  getters,
  state,
  actions,
  mutations
}
